package com.yjhb.android.sbz.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.BalanceDetailed;
import com.jojo.android.zxlib.base.BaseListAdapter;
import com.jojo.android.zxlib.tool.ZxTimeTool;

/**
 * 创建者 zx
 * 创建时间 2020/12/4 15:14
 */
public class BalanceAdapter extends BaseListAdapter<BalanceDetailed> {
    private final String TAG = "BalanceAdapter";

    public BalanceAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            BalanceHolder balanceHolder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).
                        inflate(R.layout.item_balance, null, false);
                convertView.setTag(new BalanceHolder(convertView));
            }
            balanceHolder = (BalanceHolder) convertView.getTag();
            BalanceDetailed balanceDetailed = data.get(position);
            String tag = BalanceDetailed.Tag(balanceDetailed.getTag());
            if (TextUtils.isEmpty(tag)) {
                tag = balanceDetailed.getRemark();
            }
            balanceHolder.remark_tv.setText(tag);
            balanceHolder.date_tv.setText(ZxTimeTool.simpleDateFormat("MM-dd HH:mm", balanceDetailed.getCreateTime()));
            if (balanceDetailed.getType() == 1) {
                balanceHolder.money_tv.setTextColor(context.getResources().getColor(R.color.red_3b));
                balanceHolder.money_tv.setText(String.format("+%s元", balanceDetailed.getMoney()));
            } else if (balanceDetailed.getType() == 2) {
                balanceHolder.money_tv.setTextColor(context.getResources().getColor(R.color.black));
                balanceHolder.money_tv.setText(String.format("-%s元", balanceDetailed.getMoney()));
            }
            balanceHolder.task_layout.setVisibility(View.GONE);
            balanceHolder.task_id_tv.setVisibility(View.GONE);
            balanceHolder.task_name_tv.setVisibility(View.GONE);
            if (balanceDetailed.getTaskId() != null) {
                balanceHolder.task_layout.setVisibility(View.VISIBLE);
                balanceHolder.task_id_tv.setVisibility(View.VISIBLE);
                balanceHolder.task_id_tv.setText("任务ID:" + balanceDetailed.getTaskId());
            }
            if (!TextUtils.isEmpty(balanceDetailed.getTaskName())) {
                balanceHolder.task_layout.setVisibility(View.VISIBLE);
                balanceHolder.task_name_tv.setVisibility(View.VISIBLE);
                balanceHolder.task_name_tv.setText(String.format("任务名称:%s", balanceDetailed.getTaskName()));
            }

        } catch (Exception e) {
        }
        return convertView;
    }


    class BalanceHolder {


        View itemView;
        TextView remark_tv;
        TextView date_tv;
        TextView money_tv;
        View task_layout;
        TextView task_name_tv;
        TextView task_id_tv;
//        TextView state_tv;

        public BalanceHolder(@NonNull View itemView) {
            this.itemView = itemView;
            remark_tv = itemView.findViewById(R.id.remark_tv);
            date_tv = itemView.findViewById(R.id.date_tv);
            money_tv = itemView.findViewById(R.id.money_tv);
            task_layout = itemView.findViewById(R.id.task_layout);
            task_name_tv = itemView.findViewById(R.id.task_name_tv);
            task_id_tv = itemView.findViewById(R.id.task_id_tv);
//            state_tv = itemView.findViewById(R.id.state_tv);
        }
    }
}
